from flask import Blueprint, request, jsonify
import json
import logging
# from person_flask_project.utils.utils import build_fail_response, confirm_params_type
# from person_flask_project.database.db_tools import SQLServerDatabase, connection_string


from utils.utils import build_fail_response, confirm_params_type
from database.db_tools import SQLServerDatabase, connection_string

# 创建蓝图
ts_recheck_user_page = Blueprint('ts_recheck_user_page', __name__)


@ts_recheck_user_page.route('/pyapi/login', methods=['POST'])
def login():


    try:
        usercode = request.json.get('usercode')
        password = request.json.get('password')
        # 操作数据库
        with SQLServerDatabase(connection_string) as db:
            # 无需分页，查询全部
            res = db.execute_query_all("ts_recheck_user", filters="usersUserCode = ? and usersPwd = ?"
                                       , params=[usercode, password],
                                       customSelectColumns='usersUserCode as usercode, usersUserName as username')
            # print(res)
            return jsonify(res)
    except Exception as e:
        return build_fail_response(message=str(e), params=confirm_params_type(request))